package com.media.module.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 仿iphone带进度的进度条，线程安全的View，可直接在线程中更新进度
 * 
 * @author xiaanming
 *
 */
public class RoundProgressBar extends View {
	/**
	 * 画笔对象的引用
	 */
	private Paint mPaint;

	/**
	 * 圆环的颜色
	 */
	private int mintRoundColor;

	/**
	 * 圆环进度的颜色
	 */
	private int mintRoundProgressColor;

	/**
	 * 中间进度百分比的字符串的颜色
	 */
	private int mintTextColor;

	/**
	 * 中间进度百分比的字符串的字体
	 */
	private float mfTextSize;

	/**
	 * 圆环的宽度
	 */
	private float mfRoundWidth;

	/**
	 * 最大进度
	 */
	private int mintMax;

	/**
	 * 当前进度
	 */
	private int mintProgress;
	/**
	 * 是否显示中间的进度
	 */
	private boolean misTextIsDisplayable;

	/**
	 * 进度的风格，实心或者空心
	 */
	private int mintStyle = 1;

	public static final int STROKE = 0;
	public static final int FILL = 1;

	public RoundProgressBar(Context context) {
		this(context, null);
		initBar();
	}

	public RoundProgressBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		initBar();
	}

	public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initBar();

	}

	private void initBar() {
		mPaint = new Paint();

		// 获取自定义属性和默认值
		mintRoundColor = Color.GRAY;
		mintRoundProgressColor = Color.argb(255, 230, 143, 143);
		mintTextColor = Color.GREEN;
		mfTextSize = 15;
		mfRoundWidth = 2;
		mintMax = 100;
		misTextIsDisplayable = true;
		mintStyle = 1;// 默认为实心

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		/**
		 * 画最外层的大圆环
		 */
		int centre = getWidth() / 2; // 获取圆心的x坐标
		int radius = (int) (centre - mfRoundWidth / 2); // 圆环的半径
		mPaint.setColor(mintRoundColor); // 设置圆环的颜色
		mPaint.setStyle(Paint.Style.STROKE); // 设置空心
		mPaint.setStrokeWidth(mfRoundWidth); // 设置圆环的宽度
		mPaint.setAntiAlias(true); // 消除锯齿
		canvas.drawCircle(centre, centre, radius, mPaint); // 画出圆环

		Log.e("log", centre + "");

		/**
		 * 画进度百分比
		 */
		mPaint.setStrokeWidth(0);
		mPaint.setColor(mintTextColor);
		mPaint.setTextSize(mfTextSize);
		mPaint.setTypeface(Typeface.DEFAULT_BOLD); // 设置字体
		int percent = (int) (((float) mintProgress / (float) mintMax) * 100); // 中间的进度百分比，先转换成float在进行除法运算，不然都为0
		float textWidth = mPaint.measureText(percent + "%"); // 测量字体宽度，我们需要根据字体的宽度设置在圆环中间

		if (misTextIsDisplayable && percent != 0 && mintStyle == STROKE) {
			canvas.drawText(percent + "%", centre - textWidth / 2, centre + mfTextSize / 2, mPaint); // 画出进度百分比
		}

		/**
		 * 画圆弧 ，画圆环的进度
		 */

		// 设置进度是实心还是空心
		mPaint.setStrokeWidth(mfRoundWidth); // 设置圆环的宽度
		mPaint.setColor(mintRoundProgressColor); // 设置进度的颜色
		mPaint.setAlpha(160);
		RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限

		switch (mintStyle) {
		case STROKE: {
			mPaint.setStyle(Paint.Style.STROKE);
			canvas.drawArc(oval, 0, 360 * mintProgress / mintMax, false, mPaint); // 根据进度画圆弧
			break;
		}
		case FILL: {
			mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
			if (mintProgress != 0)
				canvas.drawArc(oval, 0, 360 * mintProgress / mintMax, true, mPaint); // 根据进度画圆弧
			break;
		}
		}

	}

	public synchronized int getMax() {
		return mintMax;
	}

	/**
	 * 设置进度的最大值
	 * 
	 * @param max
	 */
	public synchronized void setMax(int max) {
		if (max < 0) {
			throw new IllegalArgumentException("max not less than 0");
		}
		this.mintMax = max;
	}

	/**
	 * 获取进度.需要同步
	 * 
	 * @return
	 */
	public synchronized int getProgress() {
		return mintProgress;
	}

	/**
	 * 设置进度，此为线程安全控件，由于考虑多线的问题，需要同步 刷新界面调用postInvalidate()能在非UI线程刷新
	 * 
	 * @param progress
	 */
	public synchronized void setProgress(int progress) {
		if (progress < 0) {
			throw new IllegalArgumentException("progress not less than 0");
		}
		if (progress > mintMax) {
			progress = mintMax;
		}
		if (progress <= mintMax) {
			this.mintProgress = progress;
			postInvalidate();
		}

	}

	public int getCricleColor() {
		return mintRoundColor;
	}

	public void setCricleColor(int cricleColor) {
		this.mintRoundColor = cricleColor;
	}

	public int getCricleProgressColor() {
		return mintRoundProgressColor;
	}

	public void setCricleProgressColor(int cricleProgressColor) {
		this.mintRoundProgressColor = cricleProgressColor;
	}

	public int getTextColor() {
		return mintTextColor;
	}

	public void setTextColor(int textColor) {
		this.mintTextColor = textColor;
	}

	public float getTextSize() {
		return mfTextSize;
	}

	public void setTextSize(float textSize) {
		this.mfTextSize = textSize;
	}

	public float getRoundWidth() {
		return mfRoundWidth;
	}

	public void setRoundWidth(float roundWidth) {
		this.mfRoundWidth = roundWidth;
	}

}
